import MainHome from '../components/MainHome'
import MainMe from '../components/MainMe'
import WelcomePage from '../components/WelcomePage'
import MerchandisePage from '../components/MerchandisePage'
import UserLogin from '../components/UserLogin'

// 使用hooks
import useCheckLogin from '../hooks/useCheckLogin.js'

export default [
    {
        path: '/MainHome',
        component: MainHome,
        children: [
            {
                path: '',
                component: WelcomePage,
            },
            {
                path: 'merchandise/:id',
                component: MerchandisePage,
                // 路由守卫验证是否登录
                beforeEnter: (to, from, next) => {
                    useCheckLogin().then(check => {
                        if (check) {
                            next()
                        } else {
                            next('/UserLogin')
                        }
                    })
                }
            },
        ]
    },
    {
        path: '/Me',
        component: MainMe,
        // 路由守卫验证是否登录
        beforeEnter: (to, from, next) => {
            useCheckLogin().then(check => {
                if (check) {
                    next()
                } else {
                    next('/UserLogin')
                }
            })
        }
    },
    {
        path: '/',
        redirect: '/MainHome'
    },
    {
        path: '/UserLogin',
        component: UserLogin
    }
]